package com.fuluwa.domain.category.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fuluwa.domain.category.FlwDomainException;
import com.fuluwa.domain.category.model.Attribute;
import com.fuluwa.domain.category.model.Category;
import com.fuluwa.domain.category.repository.AttributeRepository;
import com.fuluwa.domain.category.repository.CategoryRepository;
import com.fuluwa.domain.category.repository.OptionRepository;
import com.fuluwa.domain.category.service.dto.CategoryDTO;
import com.fuluwa.domain.category.service.dto.NavDTO;
import com.fuluwa.domain.category.service.dto.NavExDTO;

@Service
public class CategoryServiceImpl implements CategoryService {

	@Autowired
	private CategoryRepository categoryDao;

	@Autowired
	private AttributeRepository attributeDao;

	@Autowired
	private OptionRepository optionDao;

	@Override
	public CategoryDTO getCategory(long cid) throws FlwDomainException {
		CategoryDTO result = new CategoryDTO();

		// 判断类目信息是否存在
		List<Category> categoryList = categoryDao.findById(cid);
		if (categoryList == null || categoryList.size() == 0) {
			throw new FlwDomainException(0x6001, "类目信息不存在"); // TODO
		}
		Category category = categoryList.get(0);

		List<Attribute> attrs = attributeDao.findById(category.getId());

		// TODO 根据属性项获取属性值选项

		result.setCategory(category);
		result.setAttrs(attrs);

		return result;
	}

	/**
	 * <ul>
	 * <li>1.获取当前的类目信息；</li>
	 * <li>2.获取全路径的类目列表；</li>
	 * <li>3.获取自类目信息列表；</li>
	 * </ul>
	 */
	@Override
	public NavDTO getNav(long cid) throws FlwDomainException {
		NavDTO nav = new NavDTO();

		// 获取当前类目信息
		Category category = categoryDao.findOne(cid);
		if (category == null) {
			throw new FlwDomainException(0x6001, "类目信息不存在"); // TODO
		}
		nav.setCategory(category);

		// 获取全类目路径
		List<Category> fullPath = new ArrayList<Category>();
		getFullPath(category, fullPath);
		nav.setFullPath(fullPath);

		// 获取子列表信息
		List<Category> childs = categoryDao.findByParent(category.getId());
		nav.setChilds(childs);
		return nav;
	}

	@Override
	public NavDTO getNavRecurs(long cid) throws FlwDomainException {
		NavDTO nav = new NavDTO();

		// 获取当前类目信息
		Category category = categoryDao.findOne(cid);
		nav.setCategory(category);

		// 获取全类目路径
		List<Category> fullPath = new ArrayList<Category>();
		getFullPath(category, fullPath);
		nav.setFullPath(fullPath.size() ==0 ? null : fullPath);

		// 递归获取子列表信息
		Category c1 = new Category();
		c1.setId(cid);
		getAllChild(c1);
		nav.setChilds(c1.getList());
		return nav;
	}

	@Override
	public NavExDTO getNavEx(long cid) throws FlwDomainException {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 获取当前类目的全路径列表
	 * 
	 * @param pcid
	 *            父类目ID
	 * @param list
	 *            全路径列表
	 */
	private void getFullPath(Category category, List<Category> list) {
		// 递归的终结态，parent为0
		if (category == null || category.getParent() == 0) {
			return;
		}

		// 增加父类目
		Category parent = categoryDao.findOne(category.getParent());
		if (parent == null) {
			throw new FlwDomainException(0x6001, "类目信息不存在"); // TODO
		}
		list.add(parent);

		getFullPath(parent, list);
	}

	/**
	 * 获取当前类目的所有子类目
	 * 
	 * @param cid
	 *            当前类目ID
	 * @param list
	 *            子类目列表
	 */
	private void getAllChild(Category category) {
		List<Category> childs = categoryDao.findByParent(category.getId());
		category.setList(childs);

		// 检查子类目列表是否为空
		if (childs == null || childs.size() == 0) {
			return;
		}

		for (Category c : childs) {
			getAllChild(c);
		}
	}

}
